<?php

namespace app\base\controller\admin\setting;

use app\base\model\logs\SmsSendLogsModel;

use app\base\model\setting\SettingSmsModel;

use app\BaseController;

use app\common\logic\DataSettingLogic;

use app\base\model\logs\SmsRechargeLogsModel;

use app\common\logic\DataLogic;

class Sms extends BaseController{

    public function show()

    {

        if(null === $sms_setting=SettingSmsModel::where([['shop_id','=',$this->request->shopId]])->find()){

            $sms_setting = new SettingSmsModel();

            $sms_setting->shop_id=$this->request->shopId;

            $sms_setting->save();

        }

        $detail=[

            'sms_num'=>$sms_setting->sms_num ?? 0,

            'integral_change_notice'=>$sms_setting->integral_change_notice ?? 0,

            'balance_change_notice'=>$sms_setting->balance_change_notice ?? 0,

            'new_order_notice'=>$sms_setting->new_order_notice ?? 0,

            'vip_balance_change_notice'=>$sms_setting->vip_balance_change_notice ?? 0,

            'boss_mobiles'=>$sms_setting->boss_mobiles ?? []

        ];

        return $this->success(['detail'=>$detail]);



    }

    

    public function getSmsPackage(){

        $DataSettingLogic = new DataSettingLogic();

        $sms = $DataSettingLogic->getData('sms');

        if(empty($sms['price'])){

            return $this->error('平台暂未配置短信充值套餐');

        }

        return $this->success([

            'price' => $sms['price'],

        ]);

    }

    

    public function getRechargeLogs(){

        $SmsRechargeLogsModel = new SmsRechargeLogsModel();

        $where = [

             ['shop_id','=',$this->request->shopId],

        ];

        $order=['sms_recharge_id' => 'desc'];

        $field="sms_recharge_id,type,info,sms_num,money,add_time";

        $append=['add_time_format'];

        $return = DataLogic::getDataList($SmsRechargeLogsModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return); 

    }



    public function getSmsSendLogs()

    {

        $SmsSendLogsModel = new SmsSendLogsModel();

        $where = [

            ['shop_id','=',$this->request->shopId],

        ];

        $order=['add_time' => 'desc'];

        $field="sms_send_id,mobile,send_type,after_sms_num,add_time";

        $append=['add_time_format'];

        $return = DataLogic::getDataList($SmsSendLogsModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }



    public function save()

    {

        $integral_change_notice=$this->request->param('integral_change_notice/d',0);

        $balance_change_notice=$this->request->param('balance_change_notice/d',0);

        $new_order_notice=$this->request->param('new_order_notice/d',0);

        $vip_balance_change_notice=$this->request->param('vip_balance_change_notice/d',0);

        $boss_mobiles=$this->request->param('boss_mobiles/a',[]);



        if(!in_array($integral_change_notice,[0,1])) return $this->error('参数错误');

        if(!in_array($balance_change_notice,[0,1])) return $this->error('参数错误');

        if(!in_array($new_order_notice,[0,1])) return $this->error('参数错误');

        if(!in_array($vip_balance_change_notice,[0,1])) return $this->error('参数错误');



        if($new_order_notice==1 || $vip_balance_change_notice==1){

            if(empty($boss_mobiles)){

                return $this->error('通知手机号不能为空');

            }

            foreach ($boss_mobiles as $k=>$mobile){

                if(!is_mobile($mobile)){

                    return $this->error('第'.($k+1).'个手机号不正确');

                }

            }

        }



        if(null === $sms_setting=SettingSmsModel::where([['shop_id','=',$this->request->shopId]])->find()){

            $sms_setting = new SettingSmsModel();

            $sms_setting->shop_id=$this->request->shopId;

        }



        $sms_setting->integral_change_notice=$integral_change_notice;

        $sms_setting->balance_change_notice=$balance_change_notice;

        $sms_setting->new_order_notice=$new_order_notice;

        $sms_setting->vip_balance_change_notice=$vip_balance_change_notice;

        $sms_setting->boss_mobiles=$boss_mobiles;

        $sms_setting->save();



        return $this->success('保存成功');



    }

}